package Zoo.View;

import Zoo.Entity.VisitorEntity;
import Zoo.Handler.VisitorViewHandler;
import Zoo.Impl.VisitorServiceImpl;
import Zoo.Service.VisitorService;
import Zoo.Util.TableDTO;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;
import java.awt.*;
import java.util.Vector;

public class VisitorView extends JFrame {
    JPanel northPanel  =new JPanel();
    JButton addBtn =new JButton("增加");
    JButton updateBtn =new JButton("修改");
    JButton delBtn =new JButton("删除");
    JTextField searchTxt=new JTextField(15);
    JButton searchBtn= new JButton("查询");

    JPanel southPanel =new JPanel();
//    JButton preBtn =new JButton("上一页");
//    JButton nextBtn =new JButton("下一页");



//    private int pageNow=1;  //当前是第几页
//    private int pageSize=10;  //一页显示多少条记录

    VisitorViewHandler visitorViewHandler;
//    public void setPageNow(int pageNow) {
//        this.pageNow = pageNow;
//
//    }
    public VisitorView(){
        super("动物园参观者界面");
        Container contentPane= getContentPane();
        visitorViewHandler =new VisitorViewHandler(this);

        //放置北边的组件
        layoutNorth(contentPane);
        //放置南边的组件
//        layoutSouth(contentPane);

//
//        Vector<Vector<Object>> data=new Vector<>();
//
//        Vector<Object> rowVector1=new Vector<>();
//        rowVector1.addElement("张三");
//        rowVector1.addElement("1");
//        rowVector1.addElement("武汉");
//
//        Vector<Object> rowVector2=new Vector<>();
//        rowVector2.addElement("李四");
//        rowVector2.addElement("2");
//        rowVector2.addElement("长安");
//
//        Vector<Object> rowVector3=new Vector<>();
//        rowVector3.addElement("王五");
//        rowVector3.addElement("3");
//        rowVector3.addElement("江门");
//
//        data.addElement(rowVector1);
//        data.addElement(rowVector2);
//        data.addElement(rowVector3);


        Vector<Vector<Object>> data = getVectors();


        //tablemodel：和jtable关联后，之后只需要更新model就能把数据的变化反应到jtable中
        VisitorTableModel visitorTableModel = VisitorTableModel.assembleModel(data);
        //jtable和table关联
        JTable jTable=new JTable(visitorTableModel);
        //设置表头
        JTableHeader tableHeader=jTable.getTableHeader();
        tableHeader.setFont(new Font(null,Font.BOLD,16));
        tableHeader.setForeground(Color.RED);

        //设置表格体
        jTable.setFont(new Font(null,Font.PLAIN,14));
        jTable.setForeground(Color.black);
        jTable.setGridColor(Color.BLACK);
        jTable.setRowHeight(30);
        //设置多行选择
        jTable.getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

        //设置表格列的渲染方式
        Vector<String> columns=visitorTableModel.getColumns();  //获取表头
        VisitorCellRender render=new VisitorCellRender();
        for(int i=0;i<columns.size();i++){
            TableColumn column = jTable.getColumn(columns.get(i));
            column.setCellRenderer(render);
        }


        JScrollPane jScrollPane=new JScrollPane(jTable);
        contentPane.add(jScrollPane);

        setSize(2000,800);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        //setResizable(false);
        setVisible(true);
    }

    //主界面和查询功能（参观者没有查询功能）
    private Vector<Vector<Object>> getVectors() {
        VisitorService visitorService=new VisitorServiceImpl();
        VisitorEntity visitorEntity=new VisitorEntity();
        TableDTO tableDTO = visitorService.retrieveVisitor(visitorEntity);
        Vector<Vector<Object>> data = tableDTO.getData();
        return data;
    }

    //放置北边组件
    private void layoutNorth(Container contentPane) {
        //增加事件监听
        addBtn.addActionListener(visitorViewHandler);
        updateBtn.addActionListener(visitorViewHandler);
        delBtn.addActionListener(visitorViewHandler);
        searchBtn.addActionListener(visitorViewHandler);

        northPanel.add(addBtn);
        northPanel.add(updateBtn);
        northPanel.add(delBtn);
        northPanel.add(searchTxt);
        northPanel.add(searchBtn);
        contentPane.add(northPanel,BorderLayout.NORTH);
    }


    //查询功能的实现
    public void reloadTable(){
        Vector<Vector<Object>> data = getVectors();
        VisitorTableModel visitorTableModel=new VisitorTableModel();
        visitorTableModel.updateModel(data);

    }

    //分页
//    private void layoutSouth(Container contentPane) {
//        southPanel.add(preBtn);
//        southPanel.add(nextBtn);
//        contentPane.add(southPanel,BorderLayout.SOUTH);
//    }

//    public static void main(String[] args) {
//        new VisitorView();
//    }
}
class VisitorCellRender extends DefaultTableCellRenderer{
    //在每一行的每一列显示之前都会调用
    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        if(row%2==0){
            setBackground(Color.LIGHT_GRAY);
        }else{
            setBackground(Color.WHITE);
        }
        setHorizontalAlignment(DefaultTableCellRenderer.CENTER);   //水平居中
        return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
    }
}

//自定义tablemodel
class VisitorTableModel extends DefaultTableModel{
    static Vector<String> columns=new Vector<>();
    //表头
    static{
        columns.addElement("动物名称");
        columns.addElement("动物种类");
        columns.addElement("动物介绍");
        columns.addElement("动物年龄");
        columns.addElement("园区id");
        columns.addElement("展区类型");
        columns.addElement("园区简介");
        columns.addElement("园区占地面积");
        columns.addElement("园区地址");
        columns.addElement("园区开始时间");
        columns.addElement("园区结束时间");
        columns.addElement("园区管理员id");

    }

    //初始的data数据为null
    public VisitorTableModel(){
        super(null,columns);
    }

    private static VisitorTableModel visitorTableModel=new VisitorTableModel();

    //组装data数据
    public static VisitorTableModel assembleModel(Vector<Vector<Object>> data){
        //组装data数据
        visitorTableModel.setDataVector(data,columns);
        return visitorTableModel;
    }

    //更新data数据
    public static void updateModel(Vector<Vector<Object>> data){
        visitorTableModel.setDataVector(data,columns);
    }

    //获取表头
    public static Vector<String> getColumns() {
        return columns;
    }

    //单格不可编辑
    @Override
    public boolean isCellEditable(int row, int column) {
        return false;
    }



}